package cn.lanqiao.dao;

import cn.lanqiao.UserDaoTest;
import cn.lanqiao.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.*;


import java.io.InputStream;

import static org.junit.Assert.*;

public class UserDaoTest1 {
    SqlSession session;
    UserDao userMapper;
    SqlSessionFactory sqlSessionFactory;

//    @BeforeClass
//    public void createFactory(){
//
//    }

    @Before
    public void setUp() throws Exception {
        //加载 mybatis 配置文件
        InputStream inputStream = UserDaoTest.class.getClassLoader().getResourceAsStream("mybatis-Config.xml");
        //构建sqlSession的工厂
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //根据 sqlSessionFactory 产生 session
        session = sqlSessionFactory.openSession();
        userMapper = session.getMapper(UserDao.class);
    }

    @After
    public void tearDown() throws Exception {
        session.close();
    }

    @Test
    public void insert() {
        User user = new User("king","456");
        userMapper.insert(user);
        session.commit();
        System.out.println(user.getUserId());
    }

    @Test
    public void query() {
        userMapper.query(0,10).stream().forEach(System.out::print);
    }

    @Test
    public void queryMap() {
        userMapper.queryMap(0,10).keySet().stream().forEach(System.out::println);
    }
}
